'''
Created on Jun 28, 2011

@author: ioan
'''
import Model.iCommand


class CommandManager(object):
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self.iCommandList = list()
        self.index = 0;

    def undo(self, nbElements):
        while nbElements >= 1 and self.index >= 0:
            iCommand = self.iCommandList[self.index]
            iCommand.undo()
            self.index -= 1
            nbElements -= 1

    def redo(self, nbElements):
        while nbElements >= 1 and self.index < len(self.iCommandList):
            iCommand = self.iCommandList[self.index]
            iCommand.execute()
            self.index += 1
            nbElements -= 1

    def addCommand(self, iCommand=Model.iCommand):
        limit = self.index + 1
        while len(self.iCommandList > limit):
            self.iCommandList.pop()
        self.iCommandList.append(iCommand)
        self.index += 1
